var current_action_name = $("#current_action_name").val();
var current_controller_name = $("#current_controller_name").val();

//jquery 初始化函数
$(function () {

    if (current_action_name == 'index'){
      data_table_init();
    }

    $("#consumer_info_submit").click(function(){
        var name        = $("#name").val();
        var mobile      = $("#mobile").val();
        var verfiy_code = $("#verfiy_code").val();

        if(name == ""){
          jyd_dialog_alert({title: "提示信息", content: "姓名不能为空！"});
          return;
        }

        if(mobile == ""){
          jyd_dialog_alert({title: "提示信息", content: "手机号不能为空！"});
          return;
        }

        if(verfiy_code == ""){
          jyd_dialog_alert({title: "提示信息", content: "验证码不能为空！"});
          return;
        }

        if (!/^1[3|4|5|8]\d{9}$/.test(mobile)){
          jyd_dialog_alert({title: "提示信息", content: "手机号不合法！"});
          return;
        }

        if (!/^\d{4}$/.test(verfiy_code)){
          jyd_dialog_alert({title: "提示信息", content: "验证码不合法！"});
          return;
        }

        var post_info         = {};
        post_info.name        = name;
        post_info.mobile      = mobile;
        post_info.verfiy_code = verfiy_code;
        console.log("执行了");
        $.post("/consumer_infos.json", post_info, function(data,status){
          if(data.result == 'success' ){
            location.href = "/consumer_infos/"+data.id
          }else{
            jyd_dialog_alert({ title: "提示信息", content: data.message });
            console.log(data.message);
          }
        });
    });

    $("#send_verfiy_code").click(function(){
      var mobile     = $("#mobile").val();
      var is_sending = $("#send_verfiy_code").attr("_is_sending");
      
      if (is_sending=='1'){
        console.log("发送中……");
        return;
      }

      if (!/^1[3|4|5|8]\d{9}$/.test(mobile)){
        jyd_dialog_alert({title: "提示信息", content: "手机号不合法！"});
        return;
      }

      $.get("/consumer_infos/verfiy_code.json?mobile="+mobile, function(data,status){
        console.log(data.message);
        send_verfiy_code();
      });
    })
});

var countdown = 60;

function send_verfiy_code(){
    if (countdown == 0) {   
        $("#send_verfiy_code").attr("_is_sending", '0'); 
        $("#send_verfiy_code").text("获取验证码");     
        countdown = 60;   
        return;  
    } else {   
        $("#send_verfiy_code").attr("_is_sending", '1');
        $("#send_verfiy_code").text("已发送（"+countdown+"s）");
        countdown--;   
    }   
    setTimeout(function() { send_verfiy_code() } ,1000);
}

function data_table_init() {

    //键盘enter事件
    document.onkeydown = function (event) {
        var e = event || window.event || arguments.callee.caller.arguments[0];
        if (e && e.keyCode == 27) { // 按 Esc
            //要做的事情
        }
        if (e && e.keyCode == 113) { // 按 F2
            //要做的事情
        }
        if (e && e.keyCode == 13) { // enter 键
            search();
        }
    };

    //表格选中事件监听
    $('#search-scope #search').click(function () {
        search();
    });

    $('#birthday').datetimepicker({
        sideBySide: true,
        locale: "zh-cn",
        format: "YYYY-MM-DD"
    });


    $('#' + current_controller_name + "_datatable").DataTable({
        pageLength: 25,
        bProcessing: true,
        bServerSide: true,
        stateSave: true,
        sAjaxSource: $('#' + current_controller_name + "_datatable").data('source'),
        language: datatable_language,
        "order": [[2, 'desc']],
        "columnDefs": [
            {"orderable": false, "targets": 0}
        ]
    });
}

function search() {
    var name = $("#search-scope #name").val();
    var mobile = $("#search-scope #mobile").val();
    var request_url = $('#' + current_controller_name + "_datatable").data('source') + "?name=" + name + "&mobile=" + mobile;
    $('#' + current_controller_name + "_datatable").DataTable().ajax.url(request_url).load()
}
